//
//  SharedViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "SharedViewController.h"


#import <UMSocialCore/UMSocialCore.h>
#import "UMSocialUIManager.h"
#import "ProgressManager.h"


@interface SharedViewController ()


@end

@implementation SharedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
    __weak typeof(self) weakSelf = self;
    
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMShareMenuSelectionView *shareSelectionView, UMSocialPlatformType platformType) {
        
        [weakSelf shareWithPlatfromType:platformType];
        //        [weakSelf shareTextToPlatformType:platformType];
        
    }];
}



//-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
//
//    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//        
//        self.modalPresentationStyle = UIModalPresentationCustom;
//        
//        self.transitioningDelegate = self;
//    }
//
//    return self;
//}

//-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
//
//    SharedPresentionController *presentVc = [[SharedPresentionController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
//    
//    _presentVc = presentVc;
//    return presentVc;
//
//}

//- (IBAction)shareSina:(id)sender {
//    
//    __weak typeof(self) weakSelf = self;
//    
//    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMShareMenuSelectionView *shareSelectionView, UMSocialPlatformType platformType) {
//       
//        [weakSelf shareWithPlatfromType:platformType];
////        [weakSelf shareTextToPlatformType:platformType];
//        
//    }];
//    
//}

//分享文本
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //设置文本
    messageObject.text = @"我分享到微博了哦";
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                NSLog(@"response message is %@",resp.message);
                //第三方原始返回的数据
                NSLog(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                NSLog(@"response data is %@",data);
            }
        }
        
//        //回到应用移除背景灰色
//        [_presentVc tapClick];
        
        [self alertWithError:error];
    }];
}
//弹出框提示
- (void)alertWithError:(NSError *)error {
    NSString *result = nil;
    if (!error) {
        result = [NSString stringWithFormat:@"恭喜你,分享成功!邀请好友一起玩转!"];
    }
    else{
        if (error) {
            result = [NSString stringWithFormat:@"分享失败,此功能正在努力开通中...code: %d\n",(int)error.code];
        }
        else{
            result = [NSString stringWithFormat:@"额,分享失败了!"];
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"越分享,越快乐!" message:result delegate:nil cancelButtonTitle:NSLocalizedString(@"确定", @"确定") otherButtonTitles:nil];
    
    [alert show];
    
    
}




-(void)shareWithPlatfromType:(UMSocialPlatformType)platformType{

    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    NSString *title = @"黑马";
    
    NSString *desc = @"我是大神";
    
    NSString *webpageUrl = @"http://www.baidu.com";
    
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:desc thumImage:[UIImage imageNamed:@"jinqi"]];
    
    //设置网页地址
    shareObject.webpageUrl = webpageUrl;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享借口
    [[UMSocialManager defaultManager]shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id result, NSError *error) {
       
        if (error) {
            
            NSLog(@"分享失败%@",error);
        }else{
        
            if ([result isKindOfClass:[UMSocialShareResponse class]]) {
                
                UMSocialShareResponse *resp = result;
                
                //分享结果消息
                NSLog(@"%@",resp.message);
                //从第三方原始返回的数据
                NSLog(@"%@",resp.originalResponse);
                
            }else{
            
                NSLog(@"%@",result);
            
            }
        
        }
        
        [self alertWithError:error];
        
    }];


}


@end


















